module.exports = function ($scope,$stateParams, $resource, $state, $http, toaster) {
  // var tcode = $stateParams.tcode
  // console.log(tcode);
  /**
   * 初始化经度
   */
  $scope.longitude = 107.301056;
  /**
   * 初始化纬度
   */
  $scope.latitude= 34.359174;
/**
       * 初始化百度地图
       */
      
      $scope.initBap = function () {
        $scope.map = new BMap.Map("container"); // 创建地图实例
        $scope.map.centerAndZoom(new BMap.Point($scope.longitude, $scope.latitude),6);  // 初始化地图，设置中心点坐标和地图级别
        $scope.map.addControl(new BMap.NavigationControl());
        $scope.map.addControl(new BMap.ScaleControl());
        $scope.map.addControl(new BMap.OverviewMapControl());
        $scope.map.enableScrollWheelZoom(true);

        var driving = new BMap.DrivingRoute($scope.map);

      $scope.addmarker = function (point,address,name,time){
          var myIcon = new BMap.Icon("http://shangke2.oss-cn-qingdao.aliyuncs.com/img/map_mark.png", new BMap.Size(21,60));
          var marker = new BMap.Marker(point,{icon:myIcon});
          var text = "当前游团为："+name+"<br/>"+"历史位置："+address+"<br/>"+"到达时间："+time;
          $scope.map.addOverlay(marker);
          $scope.createTag(marker,text);
        }
      

      $scope.addyushemarker = function (point,name){
        var myIcon = new BMap.Icon("http://shangke2.oss-cn-qingdao.aliyuncs.com/img/map_mark.png", new BMap.Size(21,60));
        var marker = new BMap.Marker(point,{icon:myIcon});
        var text = "当前地址为："+name;
        $scope.map.addOverlay(marker);
        $scope.createTag(marker,text);
      }

      $scope.addmarkergreen = function (point,address,name,time){
        var myIcon = new BMap.Icon("http://cardv2.oss-cn-hangzhou.aliyuncs.com/images/images/ba_car.png", new BMap.Size(40,40));
        var marker = new BMap.Marker(point,{icon:myIcon});
        var text = "当前游团为："+name+"<br/>"+"当前位置："+address+"<br/>"+"到达时间："+time;
        $scope.map.addOverlay(marker);
        $scope.createTag(marker,text);
      }
    
     
      $scope.createTag = function (marker,name){

        //标注
        var text =  name; 
        //给标记添加点击事件以及显示窗口信息
         var infoWindow = new BMap.InfoWindow( text );
        //  $scope.openInfoWindow(infoWindow,marker);
         marker.addEventListener("click", function () { 
           this.openInfoWindow(infoWindow);
           
         });
    }


    $resource('/api/ac/ggc/tourGroupAfter/findGtcodeList', {}, {}).save({},function (res) {
      if (res.errcode !== 0) {
        alert("数据获取失败");
        return;
      }
      $scope.dat = res.data.length;
      var resdatalist = res.data;
      $scope.objs = res.data;
      console.log(resdatalist);
      for(var q=0;q<resdatalist.length;q++){
        var resdata = resdatalist[q];
        console.log(resdata);
        var log = resdata.practical_log;
        var lat = resdata.practical_lat;
        var address = resdata.address;
        var name = resdata.tourgroup_name;
        var time = resdata.practical_time;
        var marker = new BMap.Point(log,lat);
        $scope.addmarkergreen(marker,address,name,time);
        // $scope.map.panTo(new BMap.Point(resdata.practical_log, resdata.practical_lat));
      }
    });
    
    $scope.lookdetail = function(code){
      // alert("当前点击code为："+code);
      $resource('/api/as/ggc/tourgroupafter/getTGByCode', {}, {}).save({'tourgroup_code':code},function (res) {
        if (res.errcode !== 0) {
          alert("游团信息获取失败");
          return;
        }
        $scope.detail = res.data;
      })
    }
    $scope.yushe = function(code){
      // alert("当前应该显示预设轨迹了："+code);
      $resource('/api/as/ggc/tourgroupafter/findGTList', {}, {}).save({'tourgroup_code':code},function (res) {
        if (res.errcode !== 0) {
          alert("游团信息获取失败");
          return;
        }
        $scope.map.clearOverlays();
        console.log(res.data);
        var resdata = res.data;
        for(var no in resdata){
          var log = resdata[no].center_log;
          var lat = resdata[no].center_lat;
          var name = resdata[no].center_name;
          var marker = new BMap.Point(log,lat);
          $scope.addyushemarker(marker,name);
        }
        /*以上是标点    以下是画线*/
        var driving = new BMap.DrivingRoute($scope.map);
        for(i=1;i<res.data.length;i++){
          var p1 = new BMap.Point(res.data[i-1].center_log,res.data[i-1].center_lat);
          var p2 = new BMap.Point(res.data[i].center_log,res.data[i].center_lat);
          
          driving.search(p1, p2);
          driving.setSearchCompleteCallback(function(){
            var pts = driving.getResults().getPlan(0).getRoute(0).getPath();      //通过驾车实例，获得一系列点的数组
    
            var polyline = new BMap.Polyline(pts);     
            $scope.map.addOverlay(polyline);
          })
        }
        $scope.map.panTo(new BMap.Point(resdata[0].center_log, resdata[0].center_lat));
      })
    }
    $scope.shiji = function(code){
      // alert("当前应该显示实际轨迹了："+code);
      $scope.map.clearOverlays();
      $resource('/api/ac/ggc/tourGroupAfter/findPracticalList', {}, {}).save({'tourgroup_code':code},function (res) {
        if (res.errcode !== 0) {
          alert("游团信息获取失败");
          return;
        }
        console.log(res.data);
        var resdata = res.data;
        for(var no=0;no<resdata.length;no++){
          if(no==res.data.length-1){
            var log = resdata[no].practical_log;
            var lat = resdata[no].practical_lat;
            var name = resdata[no].tourgroup_name;
            var time = resdata[no].practical_time;
            var address = resdata[no].address;
            var marker = new BMap.Point(log,lat);
            $scope.addmarkergreen(marker,address,name,time);
            continue;
          }
          var log = resdata[no].practical_log;
          var lat = resdata[no].practical_lat;
          var name = resdata[no].tourgroup_name;
          var time = resdata[no].practical_time;
          var address = resdata[no].address;
          var marker = new BMap.Point(log,lat);
          $scope.addmarker(marker,address,name,time);
        }
        /*以上是标点    以下是画线*/
        for(i=1;i<res.data.length;i++){
          var p1 = new BMap.Point(res.data[i-1].practical_log,res.data[i-1].practical_lat);
          var p2 = new BMap.Point(res.data[i].practical_log,res.data[i].practical_lat);
          
          var polyline = new BMap.Polyline([
            p1,//起始点的经纬度
            p2//终止点的经纬度
            ], {strokeColor:"red",//设置颜色 
            strokeWeight:5, //宽度
            strokeOpacity:0.3});//透明度
            $scope.map.addOverlay(polyline);
        }
        $scope.map.panTo(new BMap.Point(resdata[0].practical_log, resdata[0].practical_lat));
      })
    }
  };
      $scope.initBap();
      
      $scope.reset = function(){
        $scope.map.clearOverlays();
        $scope.initBap();
      }

  }